import mysql.connector
import sys
import os

# 添加服务器目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), 'server'))

# 从配置文件导入数据库配置
from app.utils.config import settings

# 数据库配置
db_config = {
    'host': settings.DB_HOST,
    'user': settings.DB_USER,
    'password': settings.DB_PASSWORD,
    'database': settings.DB_NAME,
    'port': settings.DB_PORT
}

try:
    # 连接到数据库
    conn = mysql.connector.connect(**db_config)
    cursor = conn.cursor()
    
    # 获取表结构
    cursor.execute("DESCRIBE tb_bbe_patient_info")
    columns = cursor.fetchall()
    
    print("Table structure for tb_bbe_patient_info:")
    print("{:<30} {:<20} {:<10} {:<10} {:<20} {:<20}".format(
        "Field", "Type", "Null", "Key", "Default", "Extra"))
    print("-" * 120)
    
    # 显示所有相关字段
    relevant_keywords = ['id', 'name', 'age', 'check', 'time', 'sex', 'phone']
    relevant_columns = [col for col in columns if any(keyword in str(col[0]).lower() for keyword in relevant_keywords)]
    
    for column in relevant_columns:
        print("{:<30} {:<20} {:<10} {:<10} {:<20} {:<20}".format(
            str(column[0]), str(column[1]), str(column[2]), 
            str(column[3]), str(column[4]), str(column[5])))
    
    # 统计字段数量
    print(f"\nTotal number of relevant columns: {len(relevant_columns)}")
    print(f"Total number of all columns: {len(columns)}")
        
    cursor.close()
    conn.close()
    
except Exception as e:
    print(f"Error: {e}")